﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace Algobox.JuicyLib.TimeSeries
{
    public class BaseTimeSeries : List<BaseTimeSeriesRecord> 
    {
        /// <summary>
        /// Adds a record to the series in thread safe manner
        /// </summary>
        /// <param name="record"></param>
        public void AddTimeSeriesRecord(BaseTimeSeriesRecord record)
        {
           this.Add(record);            
        }

        /// <summary>
        /// Gets last record
        /// </summary>
        /// <returns>BaseTimeSeriesRecord</returns>
        public BaseTimeSeriesRecord GetLastTimeSeriesRecord()
        {
            return GetLastTimeSeriesRecord(1);
        }
        /// <summary>
        /// Gets the Xth record from the back
        /// </summary>
        /// <param name="x"></param>
        /// <returns>BaseTimeSeriesRecord</returns>
        public BaseTimeSeriesRecord GetLastTimeSeriesRecord(int x)
        {
                if (this.Count == 0 || this.Count < x)
                    return null;
                else
                    return this[this.Count - x];
            
        }

        public DateTime? GetEarliestTime()
        {
            if (this.Count > 0)
                return this.First().RecordTime;
            else
                return null;
        }

        /// <summary>
        /// shallow copy of data
        /// </summary>
        /// <param name="ts"></param>
        public void ShallowCopyBTS(BaseTimeSeries ts)
        {
            ts.AddRange(this); 
        }

        /// <summary>
        /// create new timeseries records
        /// </summary>
        /// <param name="ts"></param>
        public void DeepCopyBTS(BaseTimeSeries ts)
        {
            for (int i = 0; i < this.Count; i++)
            {
                ts.Add(this[i].CreateNewCopy());  
            }
        }


        public BaseTimeSeriesRecord[] RemoveTicks(int ticks)
        {
            BaseTimeSeriesRecord[] list = null;
            
            if (ticks <= 0)
                return list;

            if (ticks >= this.Count)
            {
                ticks = this.Count;   
            }
            
            list = new BaseTimeSeriesRecord[ticks];
            this.CopyTo(0, list, 0, ticks);
            
            this.RemoveRange(0, ticks);
            
            return list;
        }

        public BaseTimeSeriesRecord[] RemoveTicks(DateTime now)
        {
            BaseTimeSeriesRecord[] list = null;
           
            int counter = 0;
            for (int i = 0; i < this.Count; i++)
            {
                if (this[i] < now)
                {
                    counter++;
                }
                else
                    break;
            }

            if (counter > 0)
            {
                list = new BaseTimeSeriesRecord[counter];
                this.CopyTo(0, list, 0, counter);
                this.RemoveRange(0, counter);
            }
            return list;
        }

    }
}
